
package com.beaut.project.service;

import com.beaut.project.dao.*;
import com.beaut.project.old.dao.OldGDMapper;
import com.beaut.project.util.StaticData;
import com.beaut.project.util.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Transactional(rollbackFor = Exception.class)
@Service("gdService")
public class GDService {
	@Autowired
	private GDMapper gdMapper;
	@Autowired
	private FinanceMapper financeMapper;
	@Autowired
	private GroupMapper groupMapper;
	@Autowired
	private WareHouseMapper warehouseMapper;
	@Autowired
	private XMAssistService xmAssistService;
	@Autowired
	private SMSMapper smsMapper;
	@Autowired
	private CRMMapper crmMapper;
	@Autowired
	private OldGDMapper oldGdMapper;
	@Autowired 
	AlarmService alarmService;
	
	public String checkSameGdCode(Map<String,Object> map){
		return String.valueOf(gdMapper.checkSameGdCode(map));
	}
	public String addGDMain(Map<String,Object> map){
    	String gd_id=UUID.randomUUID().toString();
    	String gd_content_id=UUID.randomUUID().toString();
    	map.put("gd_id", gd_id);
    	map.put("gd_content_id", gd_content_id);
    	map.put("gd_create_time", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
    	map.put("gd_content_num", 1);
    	map.put("gd_status", StaticData.START);
    	gdMapper.addGDBase(map);
    	map.put("gd_content_code", 1);
    	map.put("status", StaticData.NEW_CREATE);
    	gdMapper.addGDContent(map);
    	
    	List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
    	List<Map<String,Object>> attList = getAttList(map, atts);
    	if(attList.size() > 0){
    		gdMapper.addGDAttachment(attList);
    	}
    	
//    	List<Map<String,String>> planList = getList("plans",map);
//    	if(planList.size() > 0){
//    		gdMapper.addGDPlan(planList);
//    	}
    	List<Map<String,String>> detailList = getList("details",map);
    	if(detailList.size() > 0){
    		gdMapper.addGDFBDetail(detailList);
    	}
    	
    	List<Map<String,String>> materialList = getList("materials",map);
    	if(materialList.size() > 0){
    		gdMapper.addGDMaterial(materialList);
    	}
    	
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addGDMainChange(Map<String,Object> map){
		Map<String,Object> mapGdBg = gdMapper.getGDBaseById(map);
		String gd_content_id=UUID.randomUUID().toString();
		map.put("gd_content_id",gd_content_id);
		map.put("gd_create_time", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
    	map.put("gd_content_code",Integer.parseInt(String.valueOf(mapGdBg.get("gd_content_num")))+ 1);
    	map.put("status", StaticData.NEW_CREATE);
    	gdMapper.addGDContent(map);

    	map.put("status", StaticData.START_ADD);
    	gdMapper.updateGDContentBg(map);
    	
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		
		List<Map<String,String>> detailList = getList("details",map);
		if(detailList.size() > 0){
			gdMapper.addGDFBDetail(detailList);
		}
		
		List<Map<String,String>> materialList = getList("materials",map);
		if(materialList.size() > 0){
			gdMapper.addGDMaterial(materialList);
		}
		
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addGDChangeAssume(Map<String,Object> map){
		String change_id = UUID.randomUUID().toString();
		map.put("change_id", change_id);
		map.put("operation_date", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
		map.put("status", StaticData.START);
		gdMapper.addGDChangeAssume(map);
		
		List<Map<String,String>> detailList = getAssumList("details",map);
		if(detailList.size() > 0){
			gdMapper.addGDChangeAssumeFB(detailList);
		}
		
		List<Map<String,String>> materialList = getAssumList("materials",map);
		if(materialList.size() > 0){
			gdMapper.addGDChangeAssumeMaterial(materialList);
		}
		
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateGDChangeAssume(Map<String,Object> map){
		gdMapper.updateGDChangeAssume(map);
		//先删除所有预算在重新添加
		gdMapper.deleteGDChangeAssumeFB(map);
		List<Map<String,String>> detailList = getAssumList("details",map);
		if(detailList.size() > 0){
			gdMapper.addGDChangeAssumeFB(detailList);
		}
		//先删除所有预算在重新添加
		gdMapper.deleteGDChangeAssumeMaterial(map);
		List<Map<String,String>> materialList = getAssumList("materials",map);
		if(materialList.size() > 0){
			gdMapper.addGDChangeAssumeMaterial(materialList);
		}
		
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteChangeAssume(Map<String,Object> map){
		gdMapper.deleteChangeAssume(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}

	/**
	 * 工单变更操作
	 * @param map
	 * @return
	 */
	public String addGDBgMain(Map<String,Object> map){
		Map<String,Object> mapGdBg = gdMapper.getGDBaseById(map);
		String gd_content_id=UUID.randomUUID().toString();
		map.put("gd_content_id",gd_content_id);
    	map.put("gd_create_time", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
    	map.put("gd_content_num", 1);
    	map.put("gd_status", StaticData.WORKING);
    	map.put("gd_content_code",Integer.parseInt(String.valueOf(mapGdBg.get("gd_content_num")))+ 1);
    	map.put("status", StaticData.NEW_CREATE);
    	gdMapper.addGDContent(map);
    	map.put("status", StaticData.START_ADD);
    	gdMapper.updateGDContentBg(map);
    	List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
    	List<Map<String,Object>> attList = getAttList(map, atts);
    	if(attList.size() > 0){
    		gdMapper.addGDAttachment(attList);
    	}    	
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateGDBgMain(Map<String,Object> map){
		gdMapper.updateGDContent(map);
		//先删除工单内容的附件，然后在重新插入
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("business_id", map.get("gd_content_id"));
		paramMap.put("business_type", StaticData.GD_CONTENT);
		gdMapper.deleteGDAttachment(paramMap);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String deleteGd(Map<String,Object> map){
		if("1".equals(map.get("gd_content_code"))){
			gdMapper.deleteGDMain(map);
			gdMapper.deleteGDMainContent(map);
		}else{
			map.put("status", StaticData.WORKING);
	    	gdMapper.updateGDContentBg1(map);
	    	map.put("business_type", StaticData.GD_CONTENT);
			gdMapper.deleteGDMainContent1(map);
		}
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteGdInfo(Map<String,Object> map){
		gdMapper.deleteGdInfo(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String updateGDMain(Map<String,Object> map){
		gdMapper.updateGDBase(map);
		String gd_content_id = String.valueOf(map.get("gd_content_id"));
		gdMapper.updateGDContent(map);
		//先删除工单内容的附件，然后在重新插入
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("business_id", gd_content_id);
		paramMap.put("business_type", StaticData.GD_CONTENT);
		gdMapper.deleteGDAttachment(paramMap);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		/**
		//先删除所有任务计划在重新添加
		gdMapper.deleteGDPlan(map);
		List<Map<String,String>> planList = getList("plans",map);
		if(planList.size() > 0){
			gdMapper.addGDPlan(planList);
		}
		**/
		//先删除所有预算在重新添加
		gdMapper.deleteGDFBDetail(map);
		List<Map<String,String>> detailList = getList("details",map);
		if(detailList.size() > 0){
			gdMapper.addGDFBDetail(detailList);
		}
		//先删除所有预算在重新添加
		gdMapper.deleteGDMaterial(map);
		List<Map<String,String>> materialList = getList("materials",map);
    	if(materialList.size() > 0){
    		gdMapper.addGDMaterial(materialList);
    	}
		
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String listGDStatus(){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.START));
		map.put("text", StaticData.GD_STATUS.get(String.valueOf(StaticData.START)));
		list.add(map);
		
		map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.START_ADD));
		map.put("text", StaticData.GD_STATUS.get(String.valueOf(StaticData.START_ADD)));
		list.add(map);
		
		map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.DESCRIPTION));
		map.put("text", StaticData.GD_STATUS.get(String.valueOf(StaticData.DESCRIPTION)));
		list.add(map);
		
		map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.DESCRIPTION_ADD));
		map.put("text", StaticData.GD_STATUS.get(String.valueOf(StaticData.DESCRIPTION_ADD)));
		list.add(map);
		
		map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.WORKING));
		map.put("text", StaticData.GD_STATUS.get(String.valueOf(StaticData.WORKING)));
		list.add(map);
		
		map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.ACCOUTING));
		map.put("text", StaticData.GD_STATUS.get(String.valueOf(StaticData.ACCOUTING)));
		list.add(map);
		
		map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.FREEZE));
		map.put("text", StaticData.GD_STATUS.get(String.valueOf(StaticData.FREEZE)));
		list.add(map);
		
//		map = new HashMap<String,Object>();
//		map.put("id", String.valueOf(StaticData.CLOSED));
//		map.put("text", StaticData.GD_STATUS.get(String.valueOf(StaticData.CLOSED)));
//		list.add(map);
		return Utility.lstMaptoJson(list);
	}
	public String getGDContentByGDId(Map<String,Object> map){
		Map<String,Object> contentMap = gdMapper.getGDContentByGDId(map);
		return Utility.toJson(contentMap);
	}
	public String getGDCustomerReturnFlowNum(Map<String,Object> map){
		Map<String,Object> contentMap = gdMapper.getGDCustomerReturnFlowNum(map);
		if(contentMap == null){
			return "no";
		}else{
			return String.valueOf(contentMap.get("return_num"));
		}
	}
	public String getDbBorrowMaterialNum(Map<String,Object> map){
		Map<String,Object> contentMap = gdMapper.getDbBorrowMaterialNum(map);
		if(contentMap == null){
			return "no";
		}else{
			return String.valueOf(contentMap.get("num"));
		}
	}
	public String getGdAssMaterial(Map<String,Object> map){
		Map<String,Object> contentMap = gdMapper.getGdAssMaterial(map);
			return Utility.toJson(contentMap);
	}
	public String getDbBorrowMaterial(Map<String,Object> map){
		Map<String,Object> contentMap = gdMapper.getDbBorrowMaterial(map);
		int loan = gdMapper.checkGDLoan(map); //员工借款是否已经全部报账
		if(loan > 0){
			return "2";
		}
		int  bcg = gdMapper.getBcgMaterial(map);//补采购单
		if(bcg>0){
			return "3";
		}
		if(Integer.parseInt(String.valueOf(contentMap.get("num")))>0){
			return "1";
		}
		return null;
	}
	public String getSelfMaterialReturnNum(Map<String,Object> map){
		Map<String,Object> contentMap = gdMapper.getSelfMaterialReturnNum(map);
		return Utility.toJson(contentMap);

	}
	public String getGDControl(Map<String,Object> map){
		List<Object> list = gdMapper.getGDControl(map);
		if(list != null && list.size() > 0){
			return String.valueOf(list.get(0));
		}
		return "0";
	}
	public String getGDChangeControl(Map<String,Object> map){
		return Utility.toJson(gdMapper.getGDChangeControl(map));
	}
	public String getGDBaseById(Map<String,Object> map){
		Map<String,Object> baseMap = gdMapper.getGDBaseById(map);
		//工单内容
		Map<String,Object> contentMap = gdMapper.getGDContentByContentId(map);
		if(contentMap==null||contentMap.size()==0){
			contentMap=gdMapper.getGDContentByGDId(map);
		}
		baseMap.put("gd_content", contentMap.get("content"));
		baseMap.putAll(contentMap);
		//工单附件
		map.put("business_id", contentMap.get("gd_content_id"));
		List<Map<String,String>> attList = gdMapper.getGDAttachmentByGDId(map);
		StringBuilder attachmentId = new StringBuilder("");
		StringBuilder attachmentName = new StringBuilder("");
		if(attList != null && attList.size() > 0){
			for(int i=0;i<attList.size();i++){
				Map<String,String> m = attList.get(i);
				if(m != null){
					if(i==0){
						attachmentId.append(m.get("attachmentId"));
						attachmentName.append(m.get("attachmentName"));
					}else{
						attachmentId.append(",").append(m.get("attachmentId"));
						attachmentName.append("*").append(m.get("attachmentName"));
					}
				}
			}
		}
		baseMap.put("attachmentId", attachmentId.toString());
		baseMap.put("attachmentName", attachmentName.toString());
		
		return Utility.toJson(baseMap);
	}
	public String getGDBaseBgById(Map<String,Object> map){
		Map<String,Object> baseMap = gdMapper.getGDBaseById(map);
		//工单内容
		Map<String,Object> contentMap = gdMapper.getGDContentByContentId(map);
		baseMap.put("gd_content", contentMap.get("content"));
		baseMap.putAll(contentMap);
		if(!"1".equals(String.valueOf(contentMap.get("gd_content_code")))){
		//工单附件
		map.put("business_id", map.get("gd_content_id"));
		List<Map<String,String>> attList = gdMapper.getGDAttachmentByGDId(map);
		StringBuilder attachmentId = new StringBuilder("");
		StringBuilder attachmentName = new StringBuilder("");
		if(attList != null && attList.size() > 0){
			for(int i=0;i<attList.size();i++){
				Map<String,String> m = attList.get(i);
				if(m != null){
					if(i==0){
						attachmentId.append(m.get("attachmentId"));
						attachmentName.append(m.get("attachmentName"));
					}else{
						attachmentId.append(",").append(m.get("attachmentId"));
						attachmentName.append("*").append(m.get("attachmentName"));
					}
				}
			}
		}
		baseMap.put("attachmentId", attachmentId.toString());
		baseMap.put("attachmentName", attachmentName.toString());
		}
		
		return Utility.toJson(baseMap);
	}
	
	private List<Map<String,Object>> getAttList(Map<String,Object> map,List<String> list){
		List<Map<String,Object>> attList=new ArrayList<Map<String,Object>>();
		for(int i=0;i<list.size();i++){
			if(!Utility.isNullorEmpty(list.get(i))){
				Map<String,Object> m= new HashMap<String,Object>();
				m.put("operation_date", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
				m.put("user_id", map.get("att_user_id"));
				m.put("user_name", map.get("att_user_name"));
				m.put("gd_id", map.get("gd_id"));
				m.put("business_id", map.get("gd_content_id"));
				m.put("attachment_id", list.get(i));
				m.put("business_type", StaticData.GD_CONTENT);
				attList.add(m);
			}
		}
		return attList;
	}
	private List<Map<String,Object>> getAttList1(Map<String,Object> map,List<String> list){
		List<Map<String,Object>> attList=new ArrayList<Map<String,Object>>();
		for(int i=0;i<list.size();i++){
			if(!Utility.isNullorEmpty(list.get(i))){
				Map<String,Object> m= new HashMap<String,Object>();
				m.put("operation_date", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
				m.put("user_id", map.get("att_user_id"));
				m.put("user_name", map.get("att_user_name"));
				m.put("gd_id", map.get("gd_id"));
				m.put("business_id", map.get("gd_description_id"));
				m.put("attachment_id", list.get(i));
				m.put("business_type", StaticData.GD_DESCRIPTION);
				attList.add(m);
			}
		}
		return attList;
	}
	/**
	 * 周报附件添加获取入参
	 * @param map
	 * @param list
	 * @return
	 */
	private List<Map<String,Object>> getAttList2(Map<String,Object> map,List<String> list){
		List<Map<String,Object>> attList=new ArrayList<Map<String,Object>>();
		for(int i=0;i<list.size();i++){
			if(!Utility.isNullorEmpty(list.get(i))){
				Map<String,Object> m= new HashMap<String,Object>();
				m.put("operation_date", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
				m.put("user_id", map.get("att_user_id"));
				m.put("user_name", map.get("att_user_name"));
				m.put("gd_id", map.get("gd_id"));
				m.put("business_id", map.get("business_id"));
				m.put("attachment_id", list.get(i));
				m.put("business_type",map.get("business_type"));
				attList.add(m);
			}
		}
		return attList;
	}
	private List<Map<String,String>> getAssumList(String key,Map<String,Object> map){
		List<Map<String,String>> rtList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get(key));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("change_id", String.valueOf(map.get("change_id")));
				rtList.add(m);
			}
		}
		return rtList;
	}
	private List<Map<String,String>> getList(String key,Map<String,Object> map){
		List<Map<String,String>> rtList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get(key));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("gd_id", map.get("gd_id").toString());
				m.put("gd_content_id", map.get("gd_content_id").toString());
				if("materials".equals(key)){
					m.put("control", String.valueOf(map.get("control")));
				}
				/**
				if("plans".equals(key)){
					m.put("gd_content_id", map.get("gd_content_id").toString());
				}
				**/
				rtList.add(m);
			}
		}
		return rtList;
	}
	public String listPageGDPlan(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDPlan(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageMaterial(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageMaterial(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageChangeMaterial(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageChangeMaterial(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageChangeFBDetail(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageChangeFBDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageSupplierDB(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageSupplierDB(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGd(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGd(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("gd_status_desc", StaticData.GD_STATUS.get(String.valueOf(list.get(i).get("gd_status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageTmpGd(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageTmpGd(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("gd_status_desc", StaticData.GD_STATUS.get(String.valueOf(list.get(i).get("gd_status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGDSupplierAlreadyApplyForPurchase(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDSupplierAlreadyApplyForPurchase(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGDChangeAssume(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDChangeAssume(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("gd_status_desc", StaticData.GD_STATUS.get(String.valueOf(list.get(i).get("gd_status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGDReturn(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDReturn(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("gd_status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listGDReturnDetail(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listGDReturnDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGDBase(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDBase(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("gd_status_desc", StaticData.GD_STATUS.get(String.valueOf(list.get(i).get("gd_status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGdNoLx(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGdNoLx(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("gd_status_desc", StaticData.GD_STATUS.get(String.valueOf(list.get(i).get("gd_status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageOldGDBase(Map<String,Object> map){
		List<Map<String,Object>> list= oldGdMapper.listPageOldGDBase(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("gd_status_desc", StaticData.GD_STATUS.get(String.valueOf(list.get(i).get("gd_status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listDepartment(){
		StringBuffer sb = new StringBuffer("[");
		List<Map<String,String>> rootList = oldGdMapper.getDepartList();
		if(rootList != null && rootList.size() > 0){
			for(int i=0;i<rootList.size();i++){
				Map<String,String> m = rootList.get(i);
				if(i != 0 && i!= rootList.size()){
					sb.append(",");
				}
				sb.append("{\"id\":\"").append(m.get("id"))
				.append("\",\"text\":\"").append(m.get("name"))
				.append("\",\"attributes\":"+Utility.toJson(m))
				.append(",\"children\":[");
				m.put("parent_id", m.get("id"));
				sb.append(getChildrenDepartSys(m)).append("]}");
			}
		}
		sb.append("]");
		return sb.toString();
	}
	private String getChildrenDepartSys(Map<String,String> map){
		StringBuffer sb = new StringBuffer();
		List<Map<String,String>> nodeList = oldGdMapper.getRootDepartList(map);
		if(nodeList != null && nodeList.size() >0){
			for(int i=0;i<nodeList.size();i++){
				if(i != 0 && i!= nodeList.size()){
					sb.append(",");
				}
				Map<String,String> m = nodeList.get(i);
				sb.append("{\"id\":\"").append(m.get("id"))
				.append("\",\"text\":\"").append(m.get("name"))
				.append("\",\"attributes\":"+Utility.toJson(m))
				.append(",\"children\":[");
				map.put("parent_id", m.get("id"));
				sb.append(getChildrenDepartSys(map)).append("]}");
			}
			return sb.toString();
		}
		else{
			return "";
		}
	}
	public String listPageUser(Map<String,Object> map){
		List<Map<String,Object>> users= oldGdMapper.listPageUser(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(users));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageWarehouseGD(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageWarehouseGD(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGDStatisticDetail(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDStatisticDetail(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("gd_status_desc", StaticData.GD_STATUS.get(String.valueOf(list.get(i).get("gd_status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listGDContent(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listGDContent(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		return Utility.lstMaptoJson(list);
	}
	public String listOldGDContent(Map<String,Object> map){
		List<Map<String,Object>> list= oldGdMapper.listOldGDContent(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		return Utility.lstMaptoJson(list);
	}
	public String listPageFBDetail(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageFBDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageSelfMaterial(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageSelfMaterial(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGDDescription(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDDescription(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.DESCRIPTION_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String addGDDescription(Map<String,Object> map){
		String gd_description_id=UUID.randomUUID().toString();
		map.put("gd_description_id", gd_description_id);
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGDDescription(map);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList1(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateGDDescription(Map<String,Object> map){
		gdMapper.updateGDDescription(map);
		
		//先删除交底的附件
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("business_id", map.get("gd_description_id"));
		paramMap.put("business_type", StaticData.GD_DESCRIPTION);
		gdMapper.deleteGDAttachment(paramMap);
		
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList1(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteGdDescritpion(Map<String,Object> map){
		gdMapper.deleteGdDescritpion(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String updateGDBaseWork(Map<String,Object> map){
		List<Map<String,Object>>listStatus=gdMapper.getGDDescriptionByGdId(map);
		for(int i=0;i<listStatus.size();i++){
			if(!String.valueOf(StaticData.PASS).equals(String.valueOf(listStatus.get(i).get("status")))){
				return Utility.rtJson(Utility.RETURN_RESULT, "有未完成的交底，还请先完成交底！", null);
			}
		}
		map.put("status", StaticData.WORKING);
		gdMapper.updateGDBaseWork(map);
		xmAssistService.updateXMProgressStatusByGD(map);
		alarmService.gdDescriptionAlarm(map);
		return Utility.rtJson(Utility.RETURN_OK, "交底成功", null);
	}
	public String getGDDescriptionById(Map<String,Object> map){
		Map<String,Object> descriptionMap = gdMapper.getGDDescriptionById(map);
		//交底附件
		map.put("business_id", map.get("gd_description_id"));
		List<Map<String,String>> attList = gdMapper.getGDAttachmentByGDId(map);
		StringBuilder attachmentId = new StringBuilder("");
		StringBuilder attachmentName = new StringBuilder("");
		if(attList != null && attList.size() > 0){
			for(int i=0;i<attList.size();i++){
				Map<String,String> m = attList.get(i);
				if(m != null){
					if(i==0){
						attachmentId.append(m.get("attachmentId"));
						attachmentName.append(m.get("attachmentName"));
					}else{
						attachmentId.append(",").append(m.get("attachmentId"));
						attachmentName.append("*").append(m.get("attachmentName"));
					}
				}
			}
		}
		descriptionMap.put("attachmentId", attachmentId.toString());
		descriptionMap.put("attachmentName", attachmentName.toString());
		
		return Utility.toJson(descriptionMap);
	}
	
	public String listPageGDSelfRequest(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDSelfRequest(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listGDSelfRequestDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDSelfRequestDetail(map));
	}
	public String listGDJgRequestDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDJgRequestDetail(map));
	}
	public String listGDJgRequestDetailPrint(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDJgRequestDetailPrint(map));
	}
	public String getSelfRequestById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getSelfRequestById(map));
	}
	public String getXmBygdId(Map<String,Object> map){
		Map<String,Object> mapGx=gdMapper.getXmBygdId(map);
		if("".equals(String.valueOf(mapGx.get("xm_id")))){
			return Utility.rtJson(Utility.RETURN_ERROR, "该工单没有立项的项目！无法进行领料操作", null);
		}else{
			return Utility.rtJson(Utility.RETURN_OK, "", null);
		}
	}
	public String getJgRequestById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getJgRequestById(map));
	}
	public String getGDBaseAndContentById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getGDBaseAndContentById(map));
	}
	public String addGDSelfRequest(Map<String,Object> map){
		String request_id = UUID.randomUUID().toString();
		map.put("request_id", request_id);
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGDSelfRequest(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("request_id", request_id);
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDSelfRequestDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addGDJgRequest(Map<String,Object> map){
		String req_id = UUID.randomUUID().toString();
		map.put("req_id", req_id);
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGDJgRequest(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("req_id", req_id);
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDJgRequestDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateGDSelfRequest(Map<String,Object> map){
		gdMapper.updateGDSelfRequest(map);
		
		gdMapper.deleteGDSelfRequestDetail(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("request_id", String.valueOf(map.get("request_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDSelfRequestDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateGDJgRequest(Map<String,Object> map){
		gdMapper.updateGDJgRequest(map);
		
		gdMapper.deleteGDJgRequestDetail(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("req_id", String.valueOf(map.get("req_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDJgRequestDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteSelfRequest(Map<String,Object> map){
		gdMapper.deleteSelfRequest(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteJgRequest(Map<String,Object> map){
		gdMapper.deleteJgRequest(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String addGDCustomerBorrow(Map<String,Object> map){
		String borrow_id = UUID.randomUUID().toString();
		map.put("borrow_id", borrow_id);
		map.put("status", StaticData.NEW_CREATE);
		map.put("return_status", "0");
		gdMapper.addGDCustomerBorrow(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("borrow_id", borrow_id);
				m.put("customer_id", String.valueOf(map.get("customer_id")));
				m.put("return_status", "0");
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDCustomerBorrowDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addGDCustomerReturn(Map<String,Object> map){
		String return_id = UUID.randomUUID().toString();
		map.put("return_id", return_id);
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGDCustomerReturn(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("return_id", return_id);
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDCustomerReturnDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	
	public String updateGDCustomerReturn(Map<String,Object> map){
		gdMapper.updateGDCustomerReturn(map);
		gdMapper.deleteGDCustomerReturnDetail(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("return_id", String.valueOf(map.get("return_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDCustomerReturnDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateGDCustomerBorrow(Map<String,Object> map){
		gdMapper.updateGDCustomerBorrow(map);
		gdMapper.deleteGDCustomerBorrowDetail(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("borrow_id", String.valueOf(map.get("borrow_id")));
				m.put("customer_id", String.valueOf(map.get("customer_id")));
				m.put("return_status", "0");
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDCustomerBorrowDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	
	public String listPageGDCustomerBorrow(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDCustomerBorrow(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGDCustomerReturn(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDCustomerReturn(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageBorrowSet(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageBorrowSet(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageBorrowSetXm(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageBorrowSetXm(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listGDCustomerBorrowDetail(Map<String,Object> map){
		List<Map<String,Object>> list=gdMapper.listGDCustomerBorrowDetail(map);
//		for(int i=0;i<list.size();i++){
//			map.put("customer_id", list.get(i).get("customer_id"));
//			Map<String,Object>mapCust=warehouseMapper.getCustById(map);
//			list.get(i).put("customer", mapCust.get("company"));
//		}
		return Utility.lstMaptoJson(list);
	}
	public String listGDCustomerReturnDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDCustomerReturnDetail(map));
	}
	public String getCustomerBorrowById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getCustomerBorrowById(map));
	}
	public String getPurchaseNum(Map<String,Object> map){
		return Utility.toJson(gdMapper.getPurchaseNum(map));
	}
	public String getCustomerReturnById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getCustomerReturnById(map));
	}
	public String listPageProcess(Map<String,Object> map){
		List<Map<String,Object>>list=gdMapper.listPageProcess(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGdLoan(Map<String,Object> map){
		List<Map<String,Object>> list=gdMapper.listPageGdLoan(map);
		for(int i=0;i<list.size();i++){
				list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listGdYetAdvancesByYetId(Map<String,Object> map){
		List<Map<String,Object>> list=gdMapper.listGdYetAdvancesByYetId(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listGdYetAdvance(Map<String,Object> map){
		List<Map<String,Object>> list=gdMapper.listGdYetAdvance(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGdNewAdvances(Map<String,Object> map){
		List<Map<String,Object>> list=gdMapper.listPageGdNewAdvances(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGdAdvances(Map<String,Object> map){
		if("".equals(String.valueOf(map.get("xm_id")))&&"".equals(String.valueOf(map.get("xm_id")))){
			return "";
		}
		List<Map<String,Object>> list=gdMapper.listPageGdAdvances(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageJgRequestMateria(Map<String,Object> map){
		List<Map<String,Object>> list=gdMapper.listPageJgRequestMateria(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageFinanceGdAdvances(Map<String,Object> map){
		List<Map<String,Object>> list=gdMapper.listPageFinanceGdAdvances(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	
	public String listGdNewAdvancesDetail(Map<String,Object> map){
		List<Map<String,Object>> list=gdMapper.listGdNewAdvancesDetail(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGdYetAdvances(Map<String,Object> map){
		List<Map<String,Object>> list=gdMapper.listPageGdYetAdvances(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGdYetAdvancesDetail(Map<String,Object> map){
		List<Map<String,Object>> list=gdMapper.listPageGdYetAdvancesDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageSecurity(Map<String,Object> map){
		List<Map<String,Object>>list=gdMapper.listPageSecurity(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getProcessById(Map<String,Object> map){
		Map<String,Object>mapProcess=gdMapper.getProcessById(map);
		//交底附件
				mapProcess.put("business_id", map.get("id"));
				List<Map<String,String>> attList = gdMapper.getGDAttachmentByGDId(mapProcess);
				StringBuilder attachmentId = new StringBuilder("");
				StringBuilder attachmentName = new StringBuilder("");
				if(attList != null && attList.size() > 0){
					for(int i=0;i<attList.size();i++){
						Map<String,String> m = attList.get(i);
						if(m != null){
							if(i==0){
								attachmentId.append(m.get("attachmentId"));
								attachmentName.append(m.get("attachmentName"));
							}else{
								attachmentId.append(",").append(m.get("attachmentId"));
								attachmentName.append("*").append(m.get("attachmentName"));
							}
						}
					}
				}
				mapProcess.put("attachmentId", attachmentId.toString());
				mapProcess.put("attachmentName", attachmentName.toString());
				
				return Utility.toJson(mapProcess);
	}
	public String getGdLoanById(Map<String,Object> map){
		Map<String,Object>mapLoan=gdMapper.getGdLoanById(map);
		//交底附件
		mapLoan.put("business_id", map.get("id"));
		List<Map<String,String>> attList = gdMapper.getGDAttachmentByGDId(mapLoan);
		StringBuilder attachmentId = new StringBuilder("");
		StringBuilder attachmentName = new StringBuilder("");
		if(attList != null && attList.size() > 0){
			for(int i=0;i<attList.size();i++){
				Map<String,String> m = attList.get(i);
				if(m != null){
					if(i==0){
						attachmentId.append(m.get("attachmentId"));
						attachmentName.append(m.get("attachmentName"));
					}else{
						attachmentId.append(",").append(m.get("attachmentId"));
						attachmentName.append("*").append(m.get("attachmentName"));
					}
				}
			}
		}
		mapLoan.put("attachmentId", attachmentId.toString());
		mapLoan.put("attachmentName", attachmentName.toString());
		
		return Utility.toJson(mapLoan);
	}
	public String getGdNewAdvancesById(Map<String,Object> map){
		Map<String,Object>mapLoan=gdMapper.getGdNewAdvancesById(map);
		Map<String,Object> mapAlreadyMoney = crmMapper.getWorkerAdvancesMoney(mapLoan);
		Map<String,Object> mapPayMoney = crmMapper.gdWorkSumMoney(mapLoan);
		if(null!=mapAlreadyMoney){
			mapLoan.put("already_money", mapAlreadyMoney.get("already_money"));
		}else{
			mapLoan.put("already_money","0");
		}
		if(null!=mapPayMoney){
			mapLoan.put("worker_pay", mapPayMoney.get("worker_pay"));
		}else{
			mapLoan.put("worker_pay","0");
		}
		return Utility.toJson(mapLoan);
	}
	public String getGdAdvancesById(Map<String,Object> map){
		Map<String,Object>mapLoan=gdMapper.getGdAdvancesById(map);
		//交底附件
		mapLoan.put("business_id", map.get("id"));
		List<Map<String,String>> attList = gdMapper.getGDAttachmentByGDId(mapLoan);
		StringBuilder attachmentId = new StringBuilder("");
		StringBuilder attachmentName = new StringBuilder("");
		if(attList != null && attList.size() > 0){
			for(int i=0;i<attList.size();i++){
				Map<String,String> m = attList.get(i);
				if(m != null){
					if(i==0){
						attachmentId.append(m.get("attachmentId"));
						attachmentName.append(m.get("attachmentName"));
					}else{
						attachmentId.append(",").append(m.get("attachmentId"));
						attachmentName.append("*").append(m.get("attachmentName"));
					}
				}
			}
		}
		mapLoan.put("attachmentId", attachmentId.toString());
		mapLoan.put("attachmentName", attachmentName.toString());
		
		return Utility.toJson(mapLoan);
	}
	public String getGdYetAdvancesById(Map<String,Object> map){
		Map<String,Object>mapLoan=gdMapper.getGdYetAdvancesById(map);
		//交底附件
		if(mapLoan!=null){
		mapLoan.put("business_id", map.get("id"));
		List<Map<String,String>> attList = gdMapper.getGDAttachmentByGDId(mapLoan);
		StringBuilder attachmentId = new StringBuilder("");
		StringBuilder attachmentName = new StringBuilder("");
		if(attList != null && attList.size() > 0){
			for(int i=0;i<attList.size();i++){
				Map<String,String> m = attList.get(i);
				if(m != null){
					if(i==0){
						attachmentId.append(m.get("attachmentId"));
						attachmentName.append(m.get("attachmentName"));
					}else{
						attachmentId.append(",").append(m.get("attachmentId"));
						attachmentName.append("*").append(m.get("attachmentName"));
					}
				}
			}
		}
		mapLoan.put("attachmentId", attachmentId.toString());
		mapLoan.put("attachmentName", attachmentName.toString());
		StringBuilder gdIds = new StringBuilder("");
		StringBuilder gdNames = new StringBuilder("");
		List<Map<String,Object>> gdSetsList = gdMapper.getGdYetAdvances(map);
		if(gdSetsList != null && gdSetsList.size() > 0){
			for(int i=0;i<gdSetsList.size();i++){
				Map<String,Object> m = gdSetsList.get(i);
				if(m != null){
					if(i==0){
						gdIds.append(m.get("advances_id")+";"+m.get("money"));
						gdNames.append(m.get("title"));
					}else{
						gdIds.append(",").append(m.get("advances_id")+";"+m.get("money"));
						gdNames.append(",").append(m.get("title"));
					}
				}
			}
		}
		mapLoan.put("gdIds", gdIds.toString());
		mapLoan.put("gdNames", gdNames.toString());
		}
		return Utility.toJson(mapLoan);
	}
	public String getSecurityById(Map<String,Object> map){
		Map<String,Object>mapSecurity=gdMapper.getSecurityById(map);
		//交底附件
		mapSecurity.put("business_id", map.get("id"));
		List<Map<String,String>> attList = gdMapper.getGDAttachmentByGDId(mapSecurity);
		StringBuilder attachmentId = new StringBuilder("");
		StringBuilder attachmentName = new StringBuilder("");
		if(attList != null && attList.size() > 0){
			for(int i=0;i<attList.size();i++){
				Map<String,String> m = attList.get(i);
				if(m != null){
					if(i==0){
						attachmentId.append(m.get("attachmentId"));
						attachmentName.append(m.get("attachmentName"));
					}else{
						attachmentId.append(",").append(m.get("attachmentId"));
						attachmentName.append("*").append(m.get("attachmentName"));
					}
				}
			}
		}
		mapSecurity.put("attachmentId", attachmentId.toString());
		mapSecurity.put("attachmentName", attachmentName.toString());
		
		return Utility.toJson(mapSecurity);
	}
	public String deleteCustomerBorrow(Map<String,Object> map){
		gdMapper.deleteCustomerBorrow(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	
	public String deleteCustomerReturn(Map<String,Object> map){
		gdMapper.deleteCustomerReturn(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	
	public String listPageGDReuse(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDReuse(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGDPurchaseReturnApply(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDPurchaseReturnApply(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGDPurchaseApply(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDPurchaseApply(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGDSupplierApply(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDSupplierApply(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGDPurchaseApplyUse(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDPurchaseApplyUse(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listGDReuseDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDReuseDetail(map));
	}
	public String listGDPurchaseApplyDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDPurchaseApplyDetail(map));
	}
	public String listGDJgApplyDetail(Map<String,Object> map){
		if("1".equals(String.valueOf(map.get("business_type")))){
			return Utility.lstMaptoJson(gdMapper.listGDJgApplyDetail(map));
		}else{
			return Utility.lstMaptoJson(gdMapper.listGDYgApplyDetail(map));
		}
	}
	public String listGDPurchaseApplyDetail1(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDPurchaseApplyDetail1(map));
	}
	public String listGDPurchaseReturnApplyDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDPurchaseReturnApplyDetail(map));
	}
	public String listGDSupplierApplyDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDSupplierApplyDetail(map));
	}
	public String listGDSupplierApplyDetail1(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDSupplierApplyDetail1(map));
	}
	public String getPurchaseApplyDetail(Map<String,Object> map){
		return Utility.toJson(gdMapper.getPurchaseApplyDetail(map));
	}
	public String getJgMaterialTotalNum(Map<String,Object> map){
		return Utility.toJson(gdMapper.getJgMaterialTotalNum(map));
	}
	public String getCustomerMaterialTotalNum(Map<String,Object> map){
		return Utility.toJson(gdMapper.getCustomerMaterialTotalNum(map));
	}
	public String getSupplierTotalNum(Map<String,Object> map){
		return Utility.toJson(gdMapper.getSupplierTotalNum(map));
	}
	public String getSelfMaterialTotalNum(Map<String,Object> map){
		return Utility.toJson(gdMapper.getSelfMaterialTotalNum(map));
	}
	public String getSelfMaterialTotalNum1(Map<String,Object> map){
		return Utility.toJson(gdMapper.getSelfMaterialTotalNum1(map));
	}
	public String listGDPurchaseApplyUseDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDPurchaseApplyUseDetail(map));
	}
	public String addGDReuse(Map<String,Object> map){
		String reuse_id = UUID.randomUUID().toString();
		map.put("reuse_id", reuse_id);
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGDReuse(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("reuse_id", reuse_id);
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDReuseDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateGDReuse(Map<String,Object> map){
		gdMapper.updateGDReuse(map);
		gdMapper.deleteGDReuseDetail(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("reuse_id", String.valueOf(map.get("reuse_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDReuseDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteReuse(Map<String,Object> map){
		gdMapper.deleteReuse(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String getReuseById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getReuseById(map));
	}
	public String listPageGDRecovery(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDRecovery(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGDSupplierApplyForPurchase(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDSupplierApplyForPurchase(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listGDSupplierApplyForPurchaseDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDSupplierApplyForPurchaseDetail(map));
	}
	public String listGDSupplierAlreadyApplyForPurchaseDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDSupplierAlreadyApplyForPurchaseDetail(map));
	}
	public String listGDRecoveryDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDRecoveryDetail(map));
	}
	public String addGDRecovery(Map<String,Object> map){
		String recovery_id = UUID.randomUUID().toString();
		map.put("recovery_id", recovery_id);
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGDRecovery(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("recovery_id", recovery_id);
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDRecoveryDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateGDRecovery(Map<String,Object> map){
		gdMapper.updateGDRecovery(map);
		gdMapper.deleteGDRecoveryDetail(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("recovery_id", String.valueOf(map.get("recovery_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDRecoveryDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteRecovery(Map<String,Object> map){
		gdMapper.deleteRecovery(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String getRecoveryById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getRecoveryById(map));
	}
	public String getProjectMaterialNum(Map<String,Object> map){
		Map<String,Object> rtMap = gdMapper.getProjectMaterialNum(map);
		if(rtMap == null){
			return "no";
		}else{
			return String.valueOf(rtMap.get("num"));
		}
	}
	public String addProcess(Map<String,Object> map){
		String processId=UUID.randomUUID().toString();
		map.put("process_id", processId);
		map.put("business_id", processId);
		map.put("business_type", StaticData.GD_PROCESS);
		if("2".equals(String.valueOf(map.get("flag")))){
			map.put("flag", StaticData.TOPTRUE);
		}else{
			map.put("flag", StaticData.TOPFALSE);
		}
		gdMapper.addProcess(map);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	
	public String addGdLoan(Map<String,Object> map){
		String gdLoanId=UUID.randomUUID().toString();
		map.put("id", gdLoanId);
		map.put("business_id", gdLoanId);
		map.put("business_type", StaticData.GD_LOAN);
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGdLoan(map);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addGdAdvances(Map<String,Object> map){
		String gdAdvancesId=UUID.randomUUID().toString();
		map.put("id", gdAdvancesId);
		map.put("business_id", gdAdvancesId);
		map.put("business_type", StaticData.GD_ADVANCES);
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGdAdvances(map);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	
	public String addGdYetAdvances(Map<String,Object> map){
		String gdYetAdvancesId=UUID.randomUUID().toString();
		map.put("id", gdYetAdvancesId);
		map.put("yet_id", gdYetAdvancesId);
		map.put("business_id", gdYetAdvancesId);
		map.put("business_type", StaticData.GD_YET_ADVANCES);
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGdYetAdvances(map);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		String strs = String.valueOf(map.get("products"));
    	List<Map<String,String>> gdSetList = getGdsetList(map, strs);
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		if(gdSetList.size() > 0){
			gdMapper.addGdYetTempAdvances(gdSetList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	private List<Map<String,String>> getGdsetList(Map<String,Object> map,String strs ){
		List<Map<String,String>> gdSetList=new ArrayList<Map<String,String>>();
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("voucher_id", String.valueOf(map.get("yet_id")));
				m.put("advances_id", String.valueOf(m.get("advances_id")));
				m.put("money", String.valueOf(m.get("yet_money")));
				m.put("lxMoney", String.valueOf(m.get("lxMoney")));
				m.put("on_money", String.valueOf(m.get("on_money")));
				m.put("advances_type", String.valueOf(StaticData.Proof_Yet_Advances_IN));
				gdSetList.add(m);
//				Map<String,Object>mapAdv=new HashMap<String,Object>();
//				mapAdv.put("advances_id",  String.valueOf(m.get("advances_id")));
//				mapAdv.put("on_money",  String.valueOf(m.get("on_money")));
//				gdMapper.updateAdvancesOnMoneyById(mapAdv);
			}
		}
		return gdSetList;
	}
	public String updateGdLoan(Map<String,Object> map){
		//先删除附件
		map.put("business_id", map.get("id"));
		map.put("business_type", StaticData.GD_LOAN);
		gdMapper.updateGdLoan(map);
		gdMapper.deleteGDAttachment(map);
		
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateGdAdvances(Map<String,Object> map){
		//先删除附件
		map.put("business_id", map.get("id"));
		map.put("business_type", StaticData.GD_ADVANCES);
		gdMapper.updateGdAdvances(map);
		gdMapper.deleteGDAttachment(map);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateGdYetAdvances(Map<String,Object> map){
		//先删除的附件
		map.put("business_id", map.get("id"));
		map.put("yet_id", map.get("id"));
		map.put("business_type", StaticData.GD_ADVANCES);
		gdMapper.updateGdYetAdvances(map);
		gdMapper.deleteGdYetTempAdvances(map);
		gdMapper.deleteGDAttachment(map);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		String strs = String.valueOf(map.get("products"));
    	List<Map<String,String>> gdSetList = getGdsetList(map, strs);
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		if(gdSetList.size() > 0){
			gdMapper.addGdYetTempAdvances(gdSetList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateGdActualLoan(Map<String,Object> map){
		
		Map<String,Object>mapFId=new HashMap<String,Object>();
		map.put("bank_id", map.get("finance_company_id"));
		mapFId.put("id", map.get("finance_company_id"));
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		List<String> listGdCode = Arrays.asList(String.valueOf(map.get("arrGdCode")).split(","));
		Calendar now = Calendar.getInstance();
		//确认实际放款
		for(int i=0;i<list.size();i++){
			now.add(Calendar.SECOND, 1);
			String actual_operation_date = new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(now.getTime());
			Map<String,Object>mapActual=new HashMap<String,Object>();
			mapActual.put("actual_operation_date", actual_operation_date);
			mapActual.put("id", list.get(i));
			gdMapper.updateGdActualLoan(mapActual);
			/*****************公司日志添加******************/
			String f_c_log_id=UUID.randomUUID().toString();//公司账户日志主键id
			Map<String,Object> mapP = gdMapper.getGdLoanById(mapActual);//查询出借款信息
			Map<String,Object> mapF = financeMapper.getFinanceCompanyById(mapFId);//查询出公司账户信息
			Map<String,Object> mapCompany = new HashMap<String,Object>();
			mapCompany.put("f_c_log_id", f_c_log_id);//类别支出
			mapCompany.put("type", StaticData.FINANCE_OUT);//类别支出
			mapCompany.put("customer_id", mapP.get("loan_obj_id"));//借款人id
			mapCompany.put("customer", mapP.get("loan_obj_name"));//借款人
			mapCompany.put("person_id", mapP.get("person_id"));//借款人id
			mapCompany.put("person", mapP.get("person_name"));//借款人
			mapCompany.put("finance_company_id", mapFId.get("id"));//公司账户id
			mapCompany.put("finance_company_name", mapF.get("owner"));//公司账户名称
			mapCompany.put("summary","工单["+listGdCode.get(i)+"]员工借款");//摘要
			mapCompany.put("before_money", mapF.get("money"));//之前公司金额
			mapCompany.put("money", mapP.get("money"));//金额
			mapCompany.put("operation_date", actual_operation_date);//操作时间
			//要对公司账户进行扣款操作
			financeMapper.addFinanceCompanyLog(mapCompany);
			/*****************部门账户日志添加******************/
			Map<String,Object>mapD=financeMapper.getFinanceDeptByDeptId(String.valueOf(mapP.get("dept_id")));
			Map<String,Object>mapDept=new HashMap<String,Object>();
			mapDept.put("type", StaticData.FINANCE_OUT);//类别支出
			mapDept.put("proof_type", StaticData.Proof_LOAN_Out);//凭证类型
			mapDept.put("finance_dept_id", mapD.get("id"));//部门id
			mapDept.put("proof_id",mapActual.get("id"));//凭证id
			mapDept.put("before_money",mapD.get("money"));//交易之前账户金额
			mapDept.put("money",mapP.get("money"));//金额
			mapDept.put("summary","工单["+listGdCode.get(i)+"]员工借款");//摘要
			mapDept.put("person_id", mapP.get("person_id"));//经办人id
			mapDept.put("person", mapP.get("person_name"));//经办人
			mapDept.put("operation_date", actual_operation_date);//操作时间
			financeMapper.addFinanceDeptLog(mapDept);
			map.put("money", mapP.get("money"));
			map.put("dept_id",mapP.get("dept_id"));
			financeMapper.subFinanceCompanyMoney(map);
			financeMapper.subFinanceDeptMoneyByDeptId(map);
		}
	
		return Utility.rtJson(Utility.RETURN_OK, "借款发放成功", null);
	}
	public String updateGdActualAdvances(Map<String,Object> map){
		
		Map<String,Object>mapFId=new HashMap<String,Object>();
		map.put("bank_id", map.get("finance_company_id"));
		mapFId.put("id", map.get("finance_company_id"));
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		List<String> listGdCode = Arrays.asList(String.valueOf(map.get("arrGdCode")).split(","));
		Calendar now = Calendar.getInstance();
		//确认实际放款
		for(int i=0;i<list.size();i++){
			now.add(Calendar.SECOND, 1);
			String actual_operation_date = new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(now.getTime());
			Map<String,Object>mapActual=new HashMap<String,Object>();
			mapActual.put("actual_operation_date", actual_operation_date);
			mapActual.put("id", list.get(i));
			gdMapper.updateGdActualAdvances(mapActual);
			/*****************公司日志添加******************/
			String f_c_log_id=UUID.randomUUID().toString();//公司账户日志主键id
			Map<String,Object>mapP=gdMapper.getGdAdvancesById(mapActual);//查询出借款信息
			Map<String,Object> mapF=financeMapper.getFinanceCompanyById(mapFId);//查询出公司账户信息
			Map<String,Object>mapCompany=new HashMap<String,Object>();
			mapCompany.put("f_c_log_id", f_c_log_id);//类别支出
			mapCompany.put("type", StaticData.FINANCE_OUT);//类别支出
			mapCompany.put("customer_id", mapP.get("fb_advances_id"));//借款人id
			mapCompany.put("customer", mapP.get("fb_advances"));//借款人
			mapCompany.put("person_id", mapP.get("person_id"));//经办人id
			mapCompany.put("person", mapP.get("person_name"));//经办人
			mapCompany.put("finance_company_id", mapFId.get("id"));//公司账户id
			mapCompany.put("finance_company_name", mapF.get("owner"));//公司账户名称
			mapCompany.put("summary","工单["+listGdCode.get(i)+"]垫款班组");//摘要
			BigDecimal money=new BigDecimal(String.valueOf(mapF.get("money")));
			mapCompany.put("before_money", money);//之前公司金额
			mapCompany.put("money", mapP.get("money"));//金额
			mapCompany.put("operation_date", actual_operation_date);//操作时间
			//要对公司账户进行扣款操作
			financeMapper.addFinanceCompanyLog(mapCompany);
			/*****************部门账户日志添加******************/
			Map<String,Object> mapDept = new HashMap<String,Object>();
			Map<String,Object> mapD = financeMapper.getFinanceDeptByDeptId(String.valueOf(mapP.get("dept_id")));
			if(mapD != null){
				mapDept.put("type", StaticData.FINANCE_OUT);//类别支出
				mapDept.put("proof_type", StaticData.Proof_Advances_Out);//凭证类型
				mapDept.put("finance_dept_id", mapD.get("id"));//部门id
				mapDept.put("proof_id",list.get(i));//公司账户id
				mapDept.put("before_money",mapD.get("money"));//交易之前账户金额
				mapDept.put("money",mapP.get("money"));//金额
				mapDept.put("summary","工单["+listGdCode.get(i)+"]垫款班组");//摘要
				mapDept.put("person_id", mapP.get("person_id"));//经办人id
				mapDept.put("person", mapP.get("person_name"));//经办人
				mapDept.put("operation_date", actual_operation_date);//操作时间
				financeMapper.addFinanceDeptLog(mapDept);
				map.put("money", mapP.get("money"));
				map.put("dept_id",mapP.get("dept_id"));
				financeMapper.subFinanceDeptMoneyByDeptId(map);
				financeMapper.subFinanceCompanyMoney(map);
			}
		}
		
		return Utility.rtJson(Utility.RETURN_OK, "借款发放成功", null);
	}
	public String updateGdActualYetAdvances(Map<String,Object> map){
		
		Map<String,Object>mapFId=new HashMap<String,Object>();
		map.put("bank_id", map.get("finance_company_id"));
		mapFId.put("id", map.get("finance_company_id"));
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		List<String> listGdCode = Arrays.asList(String.valueOf(map.get("arrGdCode")).split(","));
		Calendar now = Calendar.getInstance();
		//确认实际放款
		for(int i=0;i<list.size();i++){
			now.add(Calendar.SECOND, 1);
			String actual_operation_date = new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(now.getTime());
			String f_c_log_id=UUID.randomUUID().toString();//公司账户日志主键id
			Map<String,Object> mapF=financeMapper.getFinanceCompanyById(mapFId);//查询出公司账户信息
			String []arrList=list.get(i).split(";");
			/*******************垫款单更新********************/
			Map<String,Object>mapActual=new HashMap<String,Object>();
			mapActual.put("actual_operation_date", actual_operation_date);
			mapActual.put("id", arrList[0]);
			mapActual.put("voucher_id", arrList[0]);
			mapActual.put("yet", 1);
			Map<String,Object>mapP=gdMapper.getGdYetAdvancesById(mapActual);//查询出借款信息
			gdMapper.updateGdActualTaYetAdvances(mapActual);
			/*****************公司日志添加******************/
			Map<String,Object>mapCompany=new HashMap<String,Object>();
			mapCompany.put("f_c_log_id", f_c_log_id);//公司日志id
			mapCompany.put("type", StaticData.FINANCE_IN);//类别支出
			mapCompany.put("customer_id", mapP.get("person_id"));//还款人id
			mapCompany.put("customer", mapP.get("person_name"));//还款人
			mapCompany.put("person_id", mapP.get("person_id"));//经办人id
			mapCompany.put("person", mapP.get("person_name"));//经办人
			mapCompany.put("finance_company_id", mapFId.get("id"));//公司账户id
			mapCompany.put("finance_company_name", mapF.get("owner"));//公司账户名称
			mapCompany.put("summary","工单["+listGdCode.get(i)+"]班组还款");//摘要
			mapCompany.put("before_money", mapF.get("money"));// 之前公司金额
			mapCompany.put("money",  arrList[1]);//金额
			mapCompany.put("operation_date", actual_operation_date);//操作时间
			//要对公司账户进行扣款操作
			financeMapper.addFinanceCompanyLog(mapCompany);
			/*****************部门账户日志添加******************/
			Map<String,Object> mapD=financeMapper.getFinanceDeptByDeptId(String.valueOf(mapP.get("dept_id")));
			Map<String,Object> mapDept=new HashMap<String,Object>();
			mapDept.put("type", StaticData.FINANCE_IN);//类别支出
			mapDept.put("proof_type", StaticData.Proof_Yet_Advances_IN);//凭证类型
			mapDept.put("proof_id", arrList[0]);//凭证类型
			mapDept.put("finance_dept_id", mapD.get("id"));//部门id
			mapDept.put("before_money",mapD.get("money"));//交易之前账户金额
			mapDept.put("money",arrList[1]);//金额
			mapDept.put("summary","工单["+listGdCode.get(i)+"]班组还款");//摘要
			mapDept.put("person_id", mapP.get("person_id"));//借款人id
			mapDept.put("person", mapP.get("person_name"));//借款人
			mapDept.put("operation_date", actual_operation_date);//操作时间
			financeMapper.addFinanceDeptLog(mapDept);
			map.put("money", arrList[1]);
			map.put("finance_dept_id",mapD.get("id"));
			financeMapper.addFinanceCompanyMoney(map);
			financeMapper.addFinanceDeptMoney(map);
			
		}
		
		return Utility.rtJson(Utility.RETURN_OK, "班组还款成功", null);
	}
	public String updateProcess(Map<String,Object> map){
		
		//先删除交底的附件
		map.put("business_id", map.get("id"));
		map.put("business_type", StaticData.GD_PROCESS);
		if("2".equals(String.valueOf(map.get("flag")))){
			map.put("flag", StaticData.TOPTRUE);
		}else{
			map.put("flag", StaticData.TOPFALSE);
		}
		gdMapper.updateProcess(map);
		gdMapper.deleteGDAttachment(map);
		
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String addSecurity(Map<String,Object> map){
		String processId=UUID.randomUUID().toString();
		map.put("seurity_id", processId);
		map.put("business_id", processId);
		map.put("business_type", StaticData.GD_SECURITY);
		gdMapper.addSecurity(map);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		alarmService.gdAlarmSecurity(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	
	public String updateSecurity(Map<String,Object> map){
		
		//先删除交底的附件
		map.put("business_id", map.get("id"));
		map.put("business_type", StaticData.GD_SECURITY);
		gdMapper.updateSecurity(map);
		gdMapper.deleteGDAttachment(map);
		
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			gdMapper.addGDAttachment(attList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteProcess(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		gdMapper.deleteProcess(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteGdLoan(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		for(int i=0;i<list.size();i++){
			Map<String,Object>mapActual=new HashMap<String,Object>();
			mapActual.put("id",String.valueOf(list.get(i)));
			Map<String,Object>mapP=gdMapper.getGdLoanById(mapActual);
			if(!String.valueOf(StaticData.NEW_CREATE).equals(String.valueOf(mapP.get("status")))){
				return Utility.rtJson(Utility.RETURN_RESULT, "正在执行中的借款单或者完结的借款单不能删除！！", null);
			}
		}
		gdMapper.deleteGdLoan(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	
	public String deleteGdNewAdvances(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		gdMapper.deleteGdNewAdvances(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteGdAdvances(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		for(int i=0;i<list.size();i++){
			Map<String,Object>mapActual=new HashMap<String,Object>();
			mapActual.put("id",String.valueOf(list.get(i)));
			Map<String,Object>mapP=gdMapper.getGdAdvancesById(mapActual);
			if(!String.valueOf(StaticData.NEW_CREATE).equals(String.valueOf(mapP.get("status")))){
				return Utility.rtJson(Utility.RETURN_RESULT, "正在执行中的还款单不能删除！！", null);
			}
		}
		gdMapper.deleteGdAdvances(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteGdYetAdvances(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		for(int i=0;i<list.size();i++){
			Map<String,Object>mapActual=new HashMap<String,Object>();
			mapActual.put("id",String.valueOf(list.get(i)));
			Map<String,Object>mapP=gdMapper.getGdYetAdvancesById(mapActual);
			if(!String.valueOf(StaticData.NEW_CREATE).equals(String.valueOf(mapP.get("status")))){
				return Utility.rtJson(Utility.RETURN_RESULT, "正在执行中的还款单不能删除！！", null);
			}
			gdMapper.deleteGdYetAdvances(mapActual);
		}
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteSecurity(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		gdMapper.deleteSecurity(list);
		map.put("business_id", map.get("id"));
		map.put("business_type", StaticData.GD_SECURITY);
		gdMapper.deleteGDAttachment(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String updateGdDescritpionStatus(Map<String,Object> map){
		gdMapper.updateGdDescritpionStatus(map);
		return Utility.rtJson(Utility.RETURN_OK, "", null);
	}
	public String updateGDStatus(Map<String,Object> map){
		gdMapper.updateGDStatus(map);
		if(StaticData.ACCOUTING ==  Integer.parseInt(String.valueOf(map.get("status")))){
			map.put("gd_actual_end", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
			gdMapper.updateGDActualEnd(map);//工单完工
			xmAssistService.updateXMProgressStatusByGD(map);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	private Object getApplyCode(Map<String,Object> map){
		List<Map<String,Object>> list = gdMapper.listGDPurchaseApply(map);
		if(list == null || list.size() == 0){
			return 1;
		}else{
			return Integer.parseInt(String.valueOf(list.get(0).get("apply_code")))+1;
		}
	}
	public String addGDPurchaseApply(Map<String,Object> map){
		Object apply_code = getApplyCode(map);
		String apply_id = UUID.randomUUID().toString();
		map.put("apply_id", apply_id);
		map.put("apply_code", apply_code);
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGDPurchaseApply(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("apply_id", apply_id);
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDPurchaseApplyDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addGDPurchaseReturnApply(Map<String,Object> map){
		String return_id = UUID.randomUUID().toString();
		Map<String,Object> gdBaseInfo = warehouseMapper.getGDBaseInfoByApplyId(map);
		map.put("gd_id", gdBaseInfo.get("gd_id"));
		map.put("gd_code", gdBaseInfo.get("gd_code"));
		map.put("gd_name", gdBaseInfo.get("gd_name"));
		map.put("dept_id", gdBaseInfo.get("gd_dept_id"));
		map.put("dept_name", gdBaseInfo.get("gd_dept_name"));
		map.put("return_id", return_id);
		map.put("purchase_id", map.get("apply_id"));
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGDPurchaseReturnApply(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("return_id", return_id);
				m.put("apply_num", String.valueOf(m.get("num")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDPurchaseReturnApplyDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addGDReturn(Map<String,Object> map){
		String return_id = UUID.randomUUID().toString();
		map.put("return_id", return_id);
		map.put("business_id", map.get("apply_id"));
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGDReturn(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("return_id", return_id);
				m.put("apply_num", String.valueOf(m.get("apply_num")));
				m.put("return_num", String.valueOf(m.get("return_num")));
				m.put("remark", String.valueOf(m.get("remark")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDReturnDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateGDReturn(Map<String,Object> map){
		gdMapper.updateGDReturn(map);
		gdMapper.deleteGdReturnApplyDetail(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("return_id", String.valueOf(map.get("return_id")));
				m.put("apply_num", String.valueOf(m.get("apply_num")));
				m.put("return_num", String.valueOf(m.get("return_num")));
				m.put("remark", String.valueOf(m.get("remark")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDReturnDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addGDNewAdvances(Map<String,Object> map){
		String main_id = UUID.randomUUID().toString();
		map.put("main_id", main_id);
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGDNewAdvances(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateGDNewAdvances(Map<String,Object> map){
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.updateGdNewAdvances(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	
	public String addGDSupplierApply(Map<String,Object> map){
		String supplier_apply_id = UUID.randomUUID().toString();
		map.put("supplier_apply_id", supplier_apply_id);
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGDSupplierApply(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("supplier_apply_id", supplier_apply_id);
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDSupplierApplyDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addGDPurchaseApplyUse(Map<String,Object> map){
		Map<String,Object> gdBaseInfo = warehouseMapper.getGDBaseInfoByApplyId(map);
		String apply_use_id = UUID.randomUUID().toString();
		map.put("apply_use_id", apply_use_id);
		map.put("gd_id", gdBaseInfo.get("gd_id"));
		map.put("gd_code", gdBaseInfo.get("gd_code"));
		map.put("gd_name", gdBaseInfo.get("gd_name"));
		map.put("dept_id", gdBaseInfo.get("gd_dept_id"));
		map.put("dept_name", gdBaseInfo.get("gd_dept_name"));
		map.put("status", StaticData.NEW_CREATE);
		gdMapper.addGDPurchaseApplyUse(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("apply_use_id", apply_use_id);
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDPurchaseApplyUseDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateGDPurchaseApplyUse(Map<String,Object> map){
		gdMapper.updateGDPurchaseApplyUse(map);
		gdMapper.deleteGDPurchaseApplyUseDetail(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("apply_use_id", String.valueOf(map.get("apply_use_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDPurchaseApplyUseDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String getPurchaseApplyById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getPurchaseApplyById(map));
	}
	public String getPurchaseReturnApplyById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getPurchaseReturnApplyById(map));
	}
	public String getReturnApplyById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getReturnApplyById(map));
	}
	public String getSupplierApplyById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getSupplierApplyById(map));
	}
	public String getPurchaseApplyUseById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getPurchaseApplyUseById(map));
	}
	
	public String updateGDPurchaseApply(Map<String,Object> map){
		gdMapper.updateGDPurchaseApply(map);
		gdMapper.deleteGDPurchaseApplyDetail(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("apply_id", String.valueOf(map.get("apply_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDPurchaseApplyDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateGDPurchaseReturnApply(Map<String,Object> map){
		gdMapper.updateGDPurchaseReturnApply(map);
		gdMapper.deleteGDPurchaseReturnApplyDetail(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("return_id", String.valueOf(map.get("return_id")));
				m.put("apply_num", String.valueOf(m.get("num")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDPurchaseReturnApplyDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateGDSupplierApply(Map<String,Object> map){
		gdMapper.updateGDSupplierApply(map);
		gdMapper.deleteGDSupplierApplyDetail(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("supplier_apply_id", String.valueOf(map.get("supplier_apply_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDSupplierApplyDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deletePurchaseApply(Map<String,Object> map){
		gdMapper.deletePurchaseApply(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deletePurchaseReturnApply(Map<String,Object> map){
		gdMapper.deletePurchaseReturnApply(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteGdReturnApply(Map<String,Object> map){
		gdMapper.deleteGdReturnApply(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteSupplierApply(Map<String,Object> map){
		gdMapper.deleteSupplierApply(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deletePurchaseApplyUse(Map<String,Object> map){
		gdMapper.deletePurchaseApplyUse(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String getSupplierPurchase(Map<String,Object> map){
		Map<String,Object> rtMap = gdMapper.getSupplierPurchase(map);
		if(rtMap == null){
			return "0";
		}else{
			return String.valueOf(rtMap.get("id"));
		}
	}
	public String listGDMaterialBalance(Map<String,Object> map){
			return Utility.lstMaptoJson(gdMapper.getGDBalanceMaterial(map));
	}
	public String getGDBalanceMaterialBefore(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.getGDBalanceMaterialBefore(map));
	}
	public String listGDMaterialBalanceByGdId(Map<String,Object> map){
		return Utility.lstMaptoJson(gdMapper.listGDMaterialBalanceByGdId(map));
	}
	public String addGDMaterialBalance1(Map<String,Object> map){
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("gd_id", String.valueOf(map.get("gd_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDMaterialBalance1(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addGDMaterialBalance(Map<String,Object> map){
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("gd_id", String.valueOf(map.get("gd_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			gdMapper.addGDMaterialBalance(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateGDMaterialBalance1(Map<String,Object> map){
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				gdMapper.updateGDMaterialBalance1(m);
			}
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateGDMaterialBalance(Map<String,Object> map){
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				gdMapper.updateGDMaterialBalance(m);
			}
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String hasBalance(Map<String,Object> map){
		return String.valueOf(gdMapper.hasBalance(map));
	}
	public String checkBalnace(Map<String,Object> map){
		map.put("type", 0);
		int balance = gdMapper.hasBalance(map);//甲供
		map.put("type",1);
		int balance1 = gdMapper.hasBalance(map);//乙供
		Map<String,Object> fbMoneyMap = gdMapper.getFBMoney(map);//分包结算
		if(fbMoneyMap != null){
			return "1";
//			if(balance == 0 && balance1 == 0){
//				return "0";
//			}else{
//				return "1";
//			}
		}else{
			return "0";
		}
	}
	public String getWorkerAndWorkerMoney(Map<String,Object> map){
		Map<String,Object> rtMap = new HashMap<String,Object>();
		List<Map<String,Object>> workerList = gdMapper.getGDWorker(map);
		rtMap.put("gd_worker_name", workerList.get(0).get("gd_worker_name"));
		rtMap.put("gd_content_id", workerList.get(0).get("gd_content_id"));
		Map<String,Object> workerMoneyMap = gdMapper.getGDWorkerMoney(map);
		if(workerMoneyMap ==  null){
			rtMap.put("worker_money", 0);
		}else{
			rtMap.put("worker_money", workerMoneyMap.get("worker_money"));
		}
		return Utility.toJson(rtMap);
	}
	public String hasFBBalance(Map<String,Object> map){
		List<Map<String,Object>> rtMap = gdMapper.hasFBBalance(map);
		if(rtMap != null && rtMap.size() > 0){
			return Utility.rtJson(Utility.RETURN_OK, "查询成功", Utility.toJson(rtMap.get(0)));
		}
		return Utility.rtJson(Utility.RETURN_ERROR, "查询失败", null);
	}
	public String addGDFBBalance(Map<String,Object> map){
		String base_id = UUID.randomUUID().toString();
		List<Map<String,Object>> workerList = gdMapper.getGDWorker(map);
		map.put("id", base_id);
		map.put("gd_worker_id", workerList.get(0).get("gd_worker_id"));
		map.put("gd_worker_name", workerList.get(0).get("gd_worker_name"));
		if(gdMapper.isExistFBBalance(map) > 0){
			return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
		}
		gdMapper.addFBBalanceMain(map);
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("base_id", base_id);
				gdMapper.updateFBBalanceDetails(m);
			}
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateGdDept(Map<String,Object> map){
//		gd_content
//		gd_customer_borrow
//		gd_customer_borrow_detail_for_self
//		gd_customer_return
//		gd_jg_request_materia
//		gd_purchase
//		gd_purchase_apply
//		gd_purchase_apply_use
//		gd_purchase_product_in
//		gd_recovery_material
//		gd_return_apply
//		gd_return_apply_use
//		gd_self_request
//		gd_self_request_fp
//		gd_supplier_apply
//		gd_supplier_purchase
//		gd_use_old_material
		oldGdMapper.updateGdDept(map);
		oldGdMapper.updateCBDept(map);
//		gdMapper.updateCbdfsDept(map);
//		oldGdMapper.updateCRDept(map);
		oldGdMapper.updateJGRMDept(map);
		oldGdMapper.updatePDept(map);
		oldGdMapper.updatePADept(map);
		oldGdMapper.updatePAUDept(map);
		oldGdMapper.updatePPIDept(map);
		oldGdMapper.updateRMDept(map);
		oldGdMapper.updateRADept(map);
		oldGdMapper.updateRAUDept(map);
		oldGdMapper.updateSRDept(map);
		oldGdMapper.updateSRFDept(map);
		oldGdMapper.updateSADept(map);
		oldGdMapper.updateSPDept(map);
		oldGdMapper.updateUOMDept(map);
		map.put("gd_bg_status", "1");
		oldGdMapper.updateGdStatus(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateGdDeptZy(Map<String,Object> map){
		List<Map<String,Object>> listGdDept = oldGdMapper.getGdDept(map);
		gdMapper.addZyGdDept(listGdDept);
		List<Map<String,Object>> listGdCDept = oldGdMapper.getGdCDept(map);
		gdMapper.addZyGdContentDept(listGdCDept);
		map.put("gd_bg_status", "2");
		oldGdMapper.updateGdStatus(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateGdDeptZying(Map<String,Object> map){
		List<Map<String,Object>> listGdDept = oldGdMapper.getGdDept(map);
		gdMapper.addZyGdDepting(listGdDept);
		List<Map<String,Object>> listGdCDept = oldGdMapper.getGdCDept(map);
		gdMapper.addZyGdContentDepting(listGdCDept);
		map.put("gd_bg_status", "2");
		oldGdMapper.updateGdStatus(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateWorkerBg(Map<String,Object> map){
		gdMapper.updateGdContentWorkerBg(map);
		gdMapper.updateFbMoney(map);
		gdMapper.updateFbPayMent(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	
	public String updateGDFBBalance(Map<String,Object> map){
		gdMapper.updateFBBalanceMain(map);
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				gdMapper.updateFBBalanceDetails(m);
			}
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String listPageFBBalanceByGdId(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageFBBalanceByGdId(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public void selfRequestPluginForward(Map<String,Object> map){
		Map<String, Object> selfRequestMap = gdMapper.getSelfRequestById(map);
		String gd_id = String.valueOf(selfRequestMap.get("gd_id"));
		String request_id = String.valueOf(map.get("id"));
		map.put("request_id", request_id);
		List<Map<String,Object>> selfRequestDetailList = gdMapper.listGDSelfRequestDetail(map);
		String dept_id = String.valueOf(selfRequestMap.get("dept_id"));
		
		for(int i = 0;i < selfRequestDetailList.size();i++){
			Map<String,Object> selfRequestDetailMap = selfRequestDetailList.get(i);
			BigDecimal num = new BigDecimal(String.valueOf(selfRequestDetailMap.get("num")));
			BigDecimal cost = new BigDecimal(String.valueOf(selfRequestDetailMap.get("cost")));
			String material_id = String.valueOf(selfRequestDetailMap.get("material_id"));
			
			//更新预算审核占用值-----start
			Map<String,Object> assumMap = new HashMap<String,Object>();
			assumMap.put("gd_id", gd_id);
			assumMap.put("material_id", material_id);
			assumMap.put("num", num);
			gdMapper.updateAssumeQueueMaterial(assumMap);
			//更新预算审核占用值-----end
			
			//1.先获取当前部门的工余库
			Map<String,Object> queryParam = new HashMap<String,Object>();
			queryParam.put("dept_id", dept_id);
			queryParam.put("type", 2);//2：工余库
			queryParam.put("product_id", material_id);
			Map<String,Object> mySelfMaterialMap= warehouseMapper.getMySelfMaterial(queryParam);
			BigDecimal myNum = null;
			//如果当前部门工余库不存在该材料，或者该材料为0，则直接从其他部门操作
			if(mySelfMaterialMap == null || new BigDecimal(String.valueOf(mySelfMaterialMap.get("num"))).compareTo(new BigDecimal("0"))==0){
				List<Map<String,Object>> otherDeptselfList = getMaterialOtherSelfDB(num, queryParam);
				for(int index=0;index < otherDeptselfList.size();index++){
					Map<String,Object> m = otherDeptselfList.get(index);
					queryParam = new HashMap<String,Object>();
					queryParam.put("db_id", m.get("db_id"));
					queryParam.put("num", m.get("num"));
					warehouseMapper.updateQueueMaterial(queryParam);
					queryParam.put("gd_id", gd_id);
					queryParam.put("request_id", request_id);
					queryParam.put("dept_id", m.get("dept_id"));
					queryParam.put("cost", cost);
					gdMapper.addSelfRequestFP(queryParam);
				}
			}else{
				myNum = new BigDecimal(String.valueOf(mySelfMaterialMap.get("num")));
				//-1表示小于，0是等于，1是大于
				if(num.compareTo(myNum) == 1){
					//1.优先本部门
					queryParam = new HashMap<String,Object>();
					queryParam.put("db_id", mySelfMaterialMap.get("db_id"));
					queryParam.put("num", myNum);
					warehouseMapper.updateQueueMaterial(queryParam);
					queryParam.put("gd_id", gd_id);
					queryParam.put("request_id", request_id);
					queryParam.put("dept_id", dept_id);
					queryParam.put("cost", cost);
					gdMapper.addSelfRequestFP(queryParam);
					//2.大于的时候，要再根据最多优先出库原则
					num = num.subtract(myNum);
					queryParam = new HashMap<String,Object>();
					queryParam.put("dept_id", dept_id);
					queryParam.put("type", 2);//2：工余库
					queryParam.put("product_id", material_id);
					List<Map<String,Object>> otherDeptselfList = getMaterialOtherSelfDB(num, queryParam);
					for(int index=0;index < otherDeptselfList.size();index++){
						Map<String,Object> m = otherDeptselfList.get(index);
						queryParam = new HashMap<String,Object>();
						queryParam.put("db_id", m.get("db_id"));
						queryParam.put("num", m.get("num"));
						warehouseMapper.updateQueueMaterial(queryParam);
						queryParam.put("gd_id", gd_id);
						queryParam.put("request_id", request_id);
						queryParam.put("dept_id", m.get("dept_id"));
						queryParam.put("cost", cost);
						gdMapper.addSelfRequestFP(queryParam);
					}
				}else{
					//小于等于的时候，直接从本部门中取
					queryParam = new HashMap<String,Object>();
					queryParam.put("db_id", mySelfMaterialMap.get("db_id"));
					queryParam.put("num", num);
					warehouseMapper.updateQueueMaterial(queryParam);
					queryParam.put("gd_id", gd_id);
					queryParam.put("request_id", request_id);
					queryParam.put("dept_id", dept_id);
					queryParam.put("cost", cost);
					gdMapper.addSelfRequestFP(queryParam);
				}
			}
		}
	}
	public void selfRequestPluginBack(Map<String,Object> map){
		Map<String, Object> selfRequestMap = gdMapper.getSelfRequestById(map);
		String gd_id = String.valueOf(selfRequestMap.get("gd_id"));
		String request_id = String.valueOf(map.get("id"));
		map.put("request_id", request_id);
		List<Map<String,Object>> selfRequestDetailList = gdMapper.listGDSelfRequestDetail(map);
		for(int i = 0;i < selfRequestDetailList.size();i++){
			Map<String,Object> selfRequestDetailMap = selfRequestDetailList.get(i);
			BigDecimal zero = new BigDecimal(0);
			BigDecimal num = new BigDecimal(String.valueOf(selfRequestDetailMap.get("num")));
			String material_id = String.valueOf(selfRequestDetailMap.get("material_id"));
			
			//更新预算审核占用值-----start
			Map<String,Object> assumMap = new HashMap<String,Object>();
			assumMap.put("gd_id", gd_id);
			assumMap.put("material_id", material_id);
			assumMap.put("num", zero.subtract(num));
			gdMapper.updateAssumeQueueMaterial(assumMap);
			//更新预算审核占用值-----end
		}
		map.put("gd_id", gd_id);
		List<Map<String,Object>> list = gdMapper.getSelfRequestFP(map);
		if(list != null && list.size() > 0){
			for(int i = 0;i < list.size();i++){
				Map<String,Object> m = list.get(i);
				BigDecimal zero = new BigDecimal(0);
				BigDecimal num = new BigDecimal(String.valueOf(m.get("num")));
				m.put("num",zero.subtract(num));
				warehouseMapper.updateQueueMaterial(m);
			}
		}
		gdMapper.deleteSelfRequestFP(map);
	}
	private List<Map<String,Object>> getMaterialOtherSelfDB(BigDecimal num,Map<String,Object> queryParam){
		List<Map<String,Object>> rtList = new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> otherDeptselfList = warehouseMapper.getMaterialOtherSelfDB(queryParam);
		for(int i=0;i<otherDeptselfList.size();i++){
			Map<String,Object> map = otherDeptselfList.get(i);
			BigDecimal otherNum = new BigDecimal(String.valueOf(map.get("num")));
			if(num.compareTo(otherNum) == 1){
				rtList.add(map);
				num = num.subtract(otherNum);
			}else{
				map.put("num",num);
				rtList.add(map);
				break;
			}
		}
		return rtList;
	}
	public String checkAssumeMaterialControl(Map<String,Object> map){
		return String.valueOf(gdMapper.checkAssumeMaterialControl(map));
	}
	public String listPageGDDel(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDDel(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("gd_status_desc", StaticData.GD_STATUS.get(String.valueOf(list.get(i).get("gd_status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
}
